<?php

class Admin_NewsController extends Zend_Controller_Action
{

    public function init()
    {    /*
                $db =$this->getFrontController()->getParam('bootstrap')->getResource('db');
            	$profiler = $db->getProfiler();
            	print_r($profiler->getQueryProfiles());
            	$query = $profiler->getLastQueryProfile();
            	echo $query->getQuery();
	  		*/
    }

    public function indexAction()
    {
        $this->_helper->redirector('list');
    }

    /**
     * 添加资讯
     */
    public function addAction()
    {
        $news = new Model_News();
        $user = Zend_Registry::get('user');
        if($this->_request->isPost()){
            $data = $this->_request->getPost();
            $data['publisher'] = $user->username;
            $util = new Hm_Util(SITE_PATH);
            $config = Zend_Registry::get('config');
            $uploadDir = rtrim($config['uploaddir'],'\/').'/images/';
            //删除非本站链接
            if(isset($data['dellink'])){
                $data['content'] = Hm_Util::deleteLink($data['content']);
            }
            //远程图片本地化
            if(isset($data['remote'])){
                $data['content'] = $util->localizeImage($data['content'], $uploadDir);
            }
            //自动将第一个图片作为资讯的缩略图，并将资讯标记为图片资讯
            $thumb = $util->firstImgAsThumb($data['content'],$config['thumb_width'],$config['thumb_height'],$uploadDir);
            if ($thumb) {
                $data['ifpic'] = 1;
                $data['thumb'] = $thumb;
            }
            $news->setData($data);
            $news->saveNews();
            $this->_helper->redirector('list');
        }else{
            $categoryList = $news->getCategoryList();
            $this->view->categoryList = $categoryList;
        }
    }

    /**
     * 单个或批量删除资讯
     */
    public function deleteAction()
    {
        $id = $this->_getParam('id');
        if(!$id){
            throw new Exception("请指定要删除的资讯！");
        }
        $newsModel = new Model_News();
        if(is_array($id)){
            $newsModel->getNewsTable()->update(array('ifdel'=>1),array('id IN (?)'=>$id));
        }else{
            $news = $newsModel->getNewsById($id);
            if($news&&$news->ifdel != 1){
                $news->ifdel = 1;
                $news->saveNews();
            }else{
                throw new Exception("您要删除的资讯不存在");
            }
        }
        $this->_helper->redirector('list');
    }

    /**
     * 编辑资讯
     */
    public function editAction()
    {
        $id = $this->_getParam('id');
        if(!$id){
            throw new Exception("请指定要修改的资讯！");
        }
        $newsModel = new Model_News();
        $news = $newsModel->getNewsById($id);
        if($news){
            if($this->_request->isPost()){
                $user = Zend_Registry::get('user');
                $data = $this->_request->getPost();
                $util = new Hm_Util(SITE_PATH);
                $config = Zend_Registry::get('config');
                $uploadDir = rtrim($config['uploaddir'],'\/').'/images/';
                //删除非本站链接
                if(isset($data['dellink'])){
                    $data['content'] = Hm_Util::deleteLink($data['content']);
                }
                //远程图片本地化
                if(isset($data['remote'])){
                    $data['content'] = $util->localizeImage($data['content'], $uploadDir);
                }
                //自动将第一个图片作为资讯的缩略图，并将资讯标记为图片资讯
                $thumb = $util->firstImgAsThumb($data['content'],$config['thumb_width'],$config['thumb_height'],$uploadDir);
                if ($thumb) {
                    $data['ifpic'] = 1;
                    $data['thumb'] = $thumb;
                }
                $news->setData($data);
                $news->last_editor = $user->username;
                $news->saveNews();
                $this->_helper->redirector('list');
            }
            $this->view->categoryList =$newsModel->getCategoryList();
            $this->view->news = $news;
        }else{
            throw new Exception("您要修改的资讯不存在");
        }
    }

    /**
     * 管理资讯
     */
    public function listAction()
    {
        $category = $this->_getParam('category');
        $pageNum = 1;
        if ($this->_getParam("page")) {
        	$pageNum = $this->_getParam("page");
        }
        $config = Zend_Registry::get('config');
        $itemsPerPage = $config['news_perPage'];
        $where = array(array('ifdel', 'and', '=', 0));
        $order = array(array('id', 'desc'));
        $news = new Model_News();
        $categoryList = $news->getCategoryList();
        if($category){
            foreach ($categoryList as $cat){
                if($cat['name'] == $category){
                    $where = array(array('cid', 'and', '=', $cat['id']));
                }
            }
        }
        $select = $news->getNewsList($where, $order);
        $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage($itemsPerPage);
        $paginator->setCurrentPageNumber($pageNum);
        $this->view->paginator = $paginator;
        $cList = array();
        foreach ($categoryList as $cat){
            $cList[$cat['id']] = $cat['name'];
        }
        $this->view->categoryList = $cList;
    }

    /**
     * 类别管理
     */
    public function categoryAction()
    {
        $news = new Model_News();
        $this->view->categoryList = $news->getCategoryList();
    }

    /**
     * 添加类别
     */
    public function caddAction()
    {
        if($this->_request->isPost()){
            $news = new Model_News();
            $data = $this->_request->getPost();
            $news->setCategoryData($data);
            $news->saveNewsCategory();
        }
        $this->_helper->redirector('category');

    }

    /**
     * 编辑类别
     */
    public function ceditAction()
    {
        $id = $this->_getParam('id');
        if(!$id){
            throw new Exception("请指定要编辑的类别！");
        }
        $newsModel = new Model_News();
        $category = $newsModel->getNewsCategoryById($id);
        if($category){
            if($this->_request->isPost()){
                $category->setCategoryData($this->_getAllParams());
                $category->saveNewsCategory();
                $this->_helper->redirector('category');
            }else{
                $this->view->category = $category;
            }
        }else{
            throw new Exception("您要编辑的类别不存在");
        }
    }

    /**
     * 删除单个或多个类别，同时删除该类别下的所有资讯
     */
    public function cdeleteAction()
    {
        $id = $this->_getParam('id');
        if(empty($id)){
            throw new Exception("请指定要删除的类别！");
        }
        $newsModel = new Model_News();
        if(is_array($id)){
            $newsModel->getNewsCategoryTable()->delete(array('id IN (?)'=>$id));
            $newsModel->getNewsTable()->delete(array('cid IN (?)'=>$id));
        }else{
            $category = $newsModel->getNewsCategoryById($id);
            if($category){
                $newsModel->getNewsCategoryTable()->delete(array('id=?'=>$id));
                $newsModel->getNewsTable()->delete(array('cid=?'=>$id));
            }else{
                throw new Exception("您要删除的类别不存在");
            }
        }
        $this->_helper->redirector('category');
    }

    /**
     * 回收站
     */
    public function recycleAction()
    {
        $category = $this->_getParam('category');
        $pageNum = 1;
        if ($this->_getParam("page")) {
        	$pageNum = $this->_getParam("page");
        }
        $config = Zend_Registry::get('config');
        $itemsPerPage = $config['news_perPage'];
        $where = array(array('ifdel', 'and', '=', 1));
        $order = array(array('id', 'desc'));
        $news = new Model_News();
        $categoryList = $news->getCategoryList();
        if($category){
            foreach ($categoryList as $cat){
                if($cat['name'] == $category){
                    $where = array(array('cid', 'and', '=', $cat['id']));
                }
            }
        }
        $select = $news->getNewsList($where, $order);
        $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage($itemsPerPage);
        $paginator->setCurrentPageNumber($pageNum);
        $this->view->paginator = $paginator;
        $cList = array();
        foreach ($categoryList as $cat){
            $cList[$cat['id']] = $cat['name'];
        }
        $this->view->categoryList = $cList;
    }

    /**
     * 还原
     */
    public function restoreAction()
    {
        $id = $this->_getParam('id');
        if(!$id){
            throw new Exception("请指定要还原的资讯！");
        }
        $newsModel = new Model_News();
        if(is_array($id)){
            $newsModel->getNewsTable()->update(array('ifdel'=>0),array('id IN (?)'=>$id));
        }else{
            $news = $newsModel->getNewsById($id);
            if($news){
                $news->ifdel = 0;
                $news->saveNews();
            }else{
                throw new Exception("您要还原的资讯不存在");
            }
        }
        $this->_helper->redirector('recycle');
    }

    /**
     * 彻底删除
     */
    public function dropAction()
    {
        $id = $this->_getParam('id');
        if(!$id){
            throw new Exception("请指定要删除的资讯！");
        }
        $newsModel = new Model_News();
        if(is_array($id)){
            $newsModel->getNewsTable()->delete(array('id IN (?)'=>$id));
        }else{
            $news = $newsModel->getNewsById($id);
            if($news){
                $news->getNewsTable()->delete(array('id=?'=>$id));
            }else{
                throw new Exception("您要删除的资讯不存在");
            }
        }
        $this->_helper->redirector('recycle');
    }
}

